﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Models;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace WebApp.Controllers
{
    public class WidgetsController : ApplicationController
    {
        //
        // GET: /Widget/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Calendar()
        {
            return View();
        }

        public ActionResult Chat()
        {
            return View();
        }

        public ActionResult StocksPreview()
        {
            UserData userData = UserData.GetInstance();
            Person user = (Person)ViewData["ProfileModel"];
            StockCollection stockCollection = userData.GetStocks(user);
            stockCollection.FetchPrices();
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<String, Stock> pair in stockCollection.Stocks)
            {
                Stock stock = pair.Value;
                sb.Append(stock.Symbol);
                sb.Append(",");
            }
            ViewData["Symbols"] = sb.ToString();
            ViewData["StocksModel"] = stockCollection;

            return PartialView("StocksPreview");
        }

        public ActionResult Stock()
        {
            String symbol = Request.QueryString["symbol"];
            if (symbol == null || symbol.Equals(""))
                return Stocks();
            ViewData["TitleFragment"] = symbol;
            UserData userData = UserData.GetInstance();
            Person user = (Person)ViewData["ProfileModel"];

            StockCollection stockCollection = userData.GetStocks(user);
            stockCollection.FetchPrices();
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<String, Stock> pair in stockCollection.Stocks)
            {
                Stock stock = pair.Value;
                sb.Append(stock.Symbol);
                sb.Append(",");
                if (pair.Key.Equals(symbol, StringComparison.CurrentCultureIgnoreCase))
                    ViewData["StockModel"] = pair.Value;
            }
            ViewData["Symbols"] = sb.ToString();
            ViewData["StocksModel"] = stockCollection;
            return View();
        }

        public ActionResult Stocks()
        {
            ViewData["TitleFragment"] = "Stocks";
            UserData userData = UserData.GetInstance();
            Person user = (Person)ViewData["ProfileModel"];

            if (Request.Form["submitBut"] != null &&
                Request.Form["action"] != null)
            {
                if (Request.Form["action"].ToString().Equals("add"))
                {
                    try
                    {
                        String symbol = Request.Form["symbol"].ToString().ToUpper();
                        double buyingPrice = Double.Parse(
                            Request.Form["buyPrice"].ToString());
                        long volume = long.Parse(Request.Form["volume"]);
                        Stock stock = new Stock(symbol);
                        stock.BuyingPrice = buyingPrice;
                        stock.Shares = volume;
                        userData.InsertStock(user, stock);
                    }
                    catch(Exception e)
                    {

                    }
                }
                else if (Request.Form["action"].ToString().Equals("save"))
                {
                    NameObjectCollectionBase.KeysCollection keys = Request.Form.Keys;
                    foreach (String key in keys)
                    {
                        Match match = Regex.Match(key, "buyPrice([A-Z.]{1,7})");
                        if (match.Success && match.Groups.Count == 2)
                        {
                            String symbol = match.Groups[1].Value;
                            try
                            {
                                double buyPrice = Double.Parse(Request.Form["buyPrice" + symbol]);
                                long volume = long.Parse(Request.Form["volume" + symbol]);
                                Stock stock = new Stock(symbol);
                                stock.BuyingPrice = buyPrice;
                                stock.Shares = volume;
                                userData.UpdateStock(user, stock);
                            }
                            catch (Exception e)
                            {

                            }
                        }
                    }
                }
                else
                {
                    Match match = Regex.Match(Request.Form["action"], "remove([A-Z.]{1,7})");
                    if (match.Success && match.Groups.Count == 2)
                    {
                        try
                        {
                            String symbol = match.Groups[1].Value;
                            Stock stock = new Stock(symbol);
                            userData.RemoveStock(user, stock);
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
            StockCollection stockCollection = userData.GetStocks(user);
            stockCollection.FetchPrices();
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<String, Stock> pair in stockCollection.Stocks)
            {
                Stock stock = pair.Value;
                sb.Append(stock.Symbol);
                sb.Append(",");
            }
            ViewData["Symbols"] = sb.ToString();
            ViewData["StocksModel"] = stockCollection;
            return View("Stocks");
        }
    }
}
